<template>
  <div class="index">
    <Header></Header>
    <ThrowSearchbox></ThrowSearchbox>
    <div class="main">
      <Searchbox class="search-box"></Searchbox>
      <div class="main-top">
        <div class="main-card">
          <div class="main-card-left">
            <span>主题市场</span>
            <ul>
              <li v-for="(item, index) in leftList" :key="index">
                <div class="item">
                  <a :href="'/Search?input=' + item[0].type_name + '&type=' + item[0].type_number">{{
                    item[0].type_name
                  }}</a
                  >/ <a :href="'/Search?input=' + item[1].type_name + '&type=' + item[1].type_number">{{ item[1].type_name }}</a
                  >/
                  <a :href="'/Search?input=' + item[2].type_name + '&type=' + item[2].type_number">{{ item[2].type_name }}</a>
                </div>
                <i class="iconfont icon-right"></i>
                <div class="box-hidden">
                  <div
                    class="box-hidden-item"
                    v-for="(item, index) in item[3]"
                    :key="index"
                  >
                    <p>{{ item[0].type_name }}</p>
                    <div class="item-content">
                      <a :href="'/Search?input=' + item[1].type_name + '&type=' + item[1].type_number">{{ item[1].type_name }}</a
                      >/ <a :href="'/Search?input=' + item[2].type_name + '&type=' + item[2].type_number">{{ item[2].type_name }}</a
                      >/
                      <a :href="'/Search?input=' + item[3].type_name + '&type=' + item[3].type_number">{{ item[3].type_name }}</a>
                    </div>
                  </div>
                </div>
              </li>
            </ul>
          </div>
          <div class="main-card-middle">
            <div class="main-card-middle-top">
              <el-carousel trigger="hover" height="280px">
                <el-carousel-item
                  v-for="(item, index) in imgMiddleList"
                  :key="index"
                >
                  <img :src="item" alt="" />
                </el-carousel-item>
              </el-carousel>
            </div>
          </div>
          <div class="main-card-middle-aside" v-if="middlesideIf">
            <img :src="imgMiddleSide" alt="" />
          </div>
          <div class="main-card-right">
            <div class="img-wrapper" @click="toHome()">
              <img :src="imgUser" alt="" />
            </div>
            <div style="text-align:center;font-size:20px" v-if="!buttonIf">余额：￥{{userInfo.balance.toFixed(2)}}</div>
            <div class="button-wrapper" v-if="buttonIf">
              <el-button type="primary" @click="toLogin()">登录</el-button>
              <el-button type="primary" @click="toRegister()">注册</el-button>
            </div>
            <div class="button-wrapper" v-else>
              <el-button type="primary" @click="charge()">充值</el-button>
              <el-input placeholder="最大1000" onkeyup = "value=value.replace(/[^\d]/g,'')" v-model="num"></el-input>
            </div>
          </div>
        </div>
        <span class="text">猜你喜欢</span>
        <div class="main-list">
          <ul class="list-ul">
            <li class="list-li" v-for="(item, index) in goodsList" :key="index" @click="toItemDetial(item)">
              <div class="img-wrapper">
                <img :src="`http://localhost:8080/api/v1/images/${item.imageDir}/thumbnail.jpg`" alt="" />
              </div>
              <div class="li-text">{{ item.description }}</div>
              <span
                ><span class="li-first">￥{{ item.price }}</span
                ><span class="li-last">{{ item.name }}</span></span
              >
            </li>
          </ul>
        </div>
        <span class="text-end">_______END_______</span>
      </div>
    </div>
    <Side v-if="sideIf"></Side>
  </div>
</template>

<script>
import Header from '@/components/Header.vue'
import Searchbox from '@/components/Searchbox.vue'
import ThrowSearchbox from '@/components/ThrowSearchbox.vue'
import Side from '@/components/Side.vue'

export default {
  name: 'Home',
  components: {
    Header,
    Searchbox,
    ThrowSearchbox,
    Side
  },
  data () {
    return {
      screenWidth: 1920,
      leftList: [],
      imgMiddleList: [
        'https://gtms02.alicdn.com/tps/i2/TB10vPXKpXXXXacXXXXvKyzTVXX-520-280.jpg',
        'https://img.alicdn.com/imgextra/i4/6000000005291/O1CN01mr9XQt1oxKHmEckLQ_!!6000000005291-0-octopus.jpg',
        'https://gtms03.alicdn.com/tps/i3/TB1gXd1JXXXXXapXpXXvKyzTVXX-520-280.jpg'
      ],
      imgMiddleSide:
        'https://img.alicdn.com/imgextra/i1/6000000001419/O1CN01X5y5RL1MLwiVoXzzA_!!6000000001419-0-octopus.jpg_290x290q90.jpg_.webp',
      middlesideIf: true,
      imgUser:
        'https://gw.alicdn.com/tps/i3/TB1yeWeIFXXXXX5XFXXuAZJYXXX-210-210.png_50x50.jpg',
      buttonIf: true,
      goodsList: [],
      sideIf: true,
      num: '',
      userInfo: {}
    }
  },
  methods: {
    toLogin () {
      this.$router.push('/Login')
    },
    toRegister () {
      this.$router.push('/Register')
    },
    toCart () {
      this.$router.push('/Cart')
    },
    toFavorite () {
      this.$router.push('/Favorite')
    },
    toHome () {
      this.$router.push('/Home')
    },
    toItemDetial (item) {
      this.$router.push({ path: '/ItemDetail', query: { item: item.itemId } })
    },
    charge () {
      if (parseInt(this.num)) {
        const userId = this.userInfo.userId
        this.userInfo.balance += parseInt(this.num)
        this.$http.put(`users/${userId}`, JSON.stringify(this.userInfo)).then((res) => {
          if (res.status === 200) {
            this.userInfo = res.data
            this.$cookie.set('userInfo', JSON.stringify(this.userInfo))
            alert('充值成功')
          } else {
            alert('失败')
          }
          this.num = ''
        }).catch((err) => {
          console.log(err.response)
        })
      }
    }
  },
  mounted () {
    this.$http.get('index/hotitems').then((res) => {
      if (res.status === 200) {
        this.goodsList = res.data
      }
    }).catch((err) => {
      console.log(err.response)
    })
    this.$http.get('index/tags').then((res) => {
      if (res.status === 200) {
        const list = res.data
        for (let i = 0; i + 7 <= list.length; i += 7) {
          this.leftList.push([list[i], list[i + 1], list[i + 2], [[list[i + 3], list[i + 4], list[i + 5], list[i + 6]]]])
        }
      }
    })
    this.screenWidth = document.body.clientWidth
    window.onresize = () => {
      return (() => {
        this.screenWidth = document.body.clientWidth
        if (this.screenWidth < 1200) {
          this.sideIf = false
          this.middlesideIf = false
        } else {
          this.sideIf = true
          this.middlesideIf = true
        }
      })()
    }
    this.userInfo = JSON.parse(this.$cookie.get('userInfo'))
    if (this.userInfo) {
      const src = this.userInfo.imgSrc
      this.buttonIf = false
      if (src) {
        this.imgUser = src
      } else {
        this.imgUser = 'https://wwc.alicdn.com/avatar/getAvatar.do?userNick=tb4186364102&width=50&height=50&type=sns&_input_charset=UTF-8'
      }
    }
  },
  watch: {
    num () {
      if (this.num < 0) {
        this.num = 0
      } else if (this.num >= 1000) {
        this.num = 1000
      }
    }
  }
}
</script>

<style lang="less" scoped>
.index {
  width: 100%;
  height: 100%;
  .main {
    width: 100%;
    background-color: rgb(234, 232, 235);
    padding: 10px 0;
    .search-box {
      margin: 20px auto;
      margin-bottom: 30px;
    }
    .main-top {
      width: 80%;
      height: 1080px;
      margin: 0 auto;
      border-radius: 20px;
      background-color: white;
      position: relative;
      .main-card {
        display: flex;
        .main-card-left {
          flex: 0 0 200px;
          text-align: center;
          span {
            font-size: 20px;
            font-weight: bold;
            line-height: 40px;
            color: #f40;
          }
          ul {
            margin: 0;
            padding: 0;
            position: relative;
            li {
              list-style: none;
              display: flex;
              font-size: 14px;
              line-height: 30px;
              &:hover {
                background-color: rgb(255, 224, 212);
                .box-hidden {
                  display: block;
                  z-index: 100;
                }
              }
              .item {
                margin: 0 auto;
                a {
                  text-decoration: none;
                  color: #666;
                  &:hover {
                    text-decoration: underline;
                    text-decoration-color: #f40;
                    color: #f40;
                  }
                }
              }
              .iconfont {
                padding-right: 5px;
                float: left;
                color: #666;
              }
              .box-hidden {
                width: 500px;
                height: 450px;
                position: absolute;
                right: -500px;
                top: 0;
                display: none;
                border: 2px solid #f40;
                border-radius: 10px;
                background-color: white;
                overflow: scroll;
                overflow-x: hidden;
                .box-hidden-item {
                  width: 40%;
                  height: 100px;
                  float: left;
                  padding-left: 20px;
                  p {
                    text-align: left;
                    padding: 0;
                    margin: 0;
                    padding-top: 10px;
                    font-size: 20px;
                  }
                  .item-content {
                    color: #666;
                    font-size: 14px;
                    text-align: left;
                    a {
                      color: #666;
                      text-decoration: none;
                    }
                  }
                }
              }
            }
          }
        }
        .main-card-middle {
          width: 500px;
          padding-top: 50px;
          padding-left: 10px;
          /deep/ .el-carousel__button {
            background-color: #f40;
          }
          .main-card-middle-top {
            .el-carousel__item {
              border-radius: 20px;
              img {
                width: 100%;
                height: 100%;
              }
            }
          }
        }
        .main-card-middle-aside {
          width: 180px;
          padding-top: 50px;
          padding-left: 10px;
          img {
            border-radius: 20px;
          }
        }
        .main-card-right {
          width: 260px;
          height: 280px;
          margin: 50px 40px;
          background-color: rgb(247, 202, 186);
          border-radius: 20px;
          .img-wrapper {
            width: 100px;
            height: 100px;
            margin: 20px auto;
            text-align: center;
            border-radius: 100%;
            overflow: hidden;
            img {
              width: 100%;
              height: 100%;
            }
          }
          .button-wrapper {
            width: 80%;
            margin: 10px auto;
            display: flex;
            justify-content: space-between;
            .el-button {
              background-color: #f40;
              border: none;
            }
          }
        }
      }
    }
    .text {
      margin: 0 50px;
      display: block;
      font-size: 20px;
      font-weight: bold;
    }
    .main-list {
      width: 100%;
      height: 620px;
      margin: 10px auto;
      overflow: hidden;
      .list-ul {
        width: 100%;
        padding: 0;
        .list-li {
          list-style: none;
          width: 220px;
          height: 280px;
          margin: 10px;
          float: left;
          background-color: white;
          border-radius: 10px;
          text-align: center;
          overflow: hidden;
          &:hover {
            box-shadow: #666 0 0 10px;
          }
          .img-wrapper {
            width: 200px;
            height: 200px;
            margin: 6px auto;
            border-radius: 10px;
            overflow: hidden;
            img {
              object-fit: cover;
              width: 100%;
            }
          }
          .li-text {
            width: 80%;
            height: 38px;
            margin: 6px auto;
            overflow: hidden;
          }
          span {
            width: 80%;
            height: 26px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            .li-first {
              font-size: 20px;
              font-weight: bold;
              color: tomato;
            }
            .li-last {
              height: 100%;
              font-size: 14px;
              line-height: 26px;
              color: #999;
            }
          }
        }
      }
    }
    .text-end {
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      font-size: 20px;
      font-weight: bold;
      color: #999;
    }
  }
}
</style>
